<?php

namespace builder\helper;

use builder\base\InvalidInstanceException;
use builder\card\HelperInterface;
use builder\card\util\CardItemHelper;

/**
 * 卡片构建助手
 */
class Card
{
    /**
     * @var HelperInterface
     */
    private static $_helper;

    /**
     * 卡片项助手
     * @return CardItemHelper
     * @throws InvalidInstanceException
     */
    public static function cardItem()
    {
        return self::get(CardItemHelper::class);
    }

    /**
     * @param string $class 助手类名
     * @return HelperInterface
     * @throws InvalidInstanceException
     */
    public static function get($class)
    {
        if (!empty(self::$_helper[$class])) {
            return self::$_helper[$class];
        }

        $object = new $class();
        if (!$object instanceof HelperInterface) {
            throw new InvalidInstanceException($class . ' must be instance of ' . HelperInterface::class);
        }

        self::$_helper[$class] = $object;
        return $object;
    }
}